<template>
  <div class="se-container">
    <div class="se-header">商品单位</div>
    <div class="se-body">
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="primary"
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
          >新增</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="success"
            icon="el-icon-edit"
            size="mini"
            :disabled="single"
            @click="handleUpdate"
          >修改</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="danger"
            icon="el-icon-delete"
            size="mini"
            :disabled="multiple"
            @click="handleDelete"
          >删除</el-button>
        </el-col>
      </el-row>

      <!--   表格   -->
      <el-table v-loading="loading" :data="unitList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column prop="id" width="55" align="center" label="id"  />
        <el-table-column label="单位名称" align="center" prop="unitName" show-overflow-tooltip/>
        <el-table-column label="创建时间" align="center" prop="createTime"          show-overflow-tooltip/>
        <el-table-column label="创建用户" align="center" prop="createBy"    show-overflow-tooltip />
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
            >修改</el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />

      <!--   添加或修改   -->
      <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
        <el-form ref="form" :model="form" :rules="rules" label-width="80px">
          <el-form-item label="单位名称" prop="unitName">
            <el-input v-model="form.unitName" placeholder="请输入商品单位名称"/>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </el-dialog>
    </div>
  </div>
</template>

<script>
  import { unitQueryList, unitQueryId, unitAdd, unitUpdate, unitDelete } from '@/api/jxc/unit'

export default {
  /** 采购管理 的 商品单位 */
  name: 'unitManager',
  data() {
    return {
      loading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      unitList: [],
      form: {},
      /** 控制弹窗 */
      open: false,
      // 弹出层标题
      title: "",
      /** 验证 */
      rules: {
        unitName: [
          { required: true, message: "商品单位名称不能为空", trigger: "blur" }
        ]
      },
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        id: undefined,
        unitName: undefined,
        createTime: undefined,
        createBy: undefined
      },
      /** 总数 */
      total: 0
    }
  },
  mounted() {
    this.getList()
  },
  methods: {
    getList() {
      unitQueryList(this.queryParams)
        .then(res => {
          this.unitList = res.data
          this.total = res.total
        })
    },
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      unitQueryId({id: id}).then(res => {
        this.form = res.data
        this.open = true
        this.title = '修改商品单位'
      })
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            unitUpdate(this.form).then(res => {
              if (res.code == 0) {
                this.$message({
                  message: '修改成功',
                  type: 'success'
                });
                this.open = false;
                this.getList();
              } else {
                this.$message({
                  message: '删除失败',
                  type: 'error'
                })
              }
            })
          } else {
            unitAdd(this.form).then(res => {
              if (res.code == 0) {
                this.$message({
                  message: '添加成功',
                  type: 'success'
                })
                this.open = false;
                this.getList();
              } else {
                this.$message({
                  message: '添加失败',
                  type: 'error'
                })
              }
            })
          }
        }
      });
    },
    handleDelete(row) {
      let delIds = row.id || this.ids;

      if (delIds instanceof Array) {
        let temp = ''
        for (let i = 0; i < delIds.length; i++) {
          let e = delIds[i]
          if (i < delIds.length - 1) e += ','
          temp += e
        }
        delIds = temp
      }
      const __this = this

      this.$confirm('是否确认删除单位id为"[' + delIds + ']"的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function() {
        unitDelete({ids: delIds}).then(res => {
          if (res.code == 0) {
            __this.$message({
              message: '删除成功',
              type: 'success'
            })
            __this.getList()
          } else {
            __this.$message({
              message: '删除失败',
              type: 'error'
            })
          }
        })
      })
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length != 1
      this.multiple = !selection.length
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        unitName: undefined,
        createTime: undefined,
        createBy: undefined
      };
    },
    /** 添加 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加商品单位";
    }
  }
}
</script>

<style lang="scss" scoped>
.se-header {
  height: 60px;
  position: relative;
  z-index: 100;
  padding: 0 20px;
  font-size: 18px;
  line-height: 60px;
}

.se-body {
  background-color: white;
  border: 1px solid #e6e6e6;
  border-radius: 2px;
}

.se-table-header {
  height: 50px;
  background-color: white;
  position: relative;
  .se-table-header-button {
    float: right;
    margin-right: 20px;
    margin-top: 10px;
  }
}

.project-reminder {
  width: auto;
  float: left;
  margin-left: 20px;
  margin-top: 10px;
}
/*@import '../styles/table.scss';*/
</style>
